package com.seo.function;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Component
public class PageTool {

	/**
	 * 自动分页
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param funList
	 * @param params
	 * @return
	 */
	public <R> PageInfo<R> getAutoPage(FunctionT1R<Map<String, Object>, List<R>> funList, Map<String, Object> params) {
		Integer pageNum = 1, pageSize = 10;
		if (params.containsKey("pageNum")) {
			pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
		}
		if (params.containsKey("pageSize")) {
			pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
		}
		PageHelper.startPage(pageNum, pageSize, true, true);
		try {
			List<R> list = funList.invoke(params);

			PageInfo<R> page = new PageInfo<>(list);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new PageInfo<>(null);
	}

	/**
	 * 自定义分页
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param funTotal
	 * @param funList
	 * @param params
	 * @return
	 */
	public <R> PageInfo<R> getZdyPage(FunctionT1R<Map<String, Object>, Integer> funTotal,
			FunctionT1R<Map<String, Object>, List<R>> funList, Map<String, Object> params) {
		try {
			Integer total = funTotal.invoke(params);
			if (total == 0) {
				return new PageInfo<>(null);
			}

			Integer pageNum = 1, pageSize = 10;

			if (params.containsKey("pageNum")) {
				pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
			}
			if (params.containsKey("pageSize")) {
				pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
			}
			int pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
			if (pageNum > pages) {
				pageNum = pages;
			}
			Page<R> page = new Page<>(pageNum, pageSize, true);
			params.put("pageSize", page.getPageSize());
			params.put("startRow", page.getStartRow());

			List<R> list = funList.invoke(params);

			page.setTotal(total);
			page.addAll(list);
			PageInfo<R> pageInfo = new PageInfo<>(page);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new PageInfo<>(null);
	}

	public <R> PageInfo<?> getZdyPage(FunctionR<Integer> funTotal, FunctionT1R<Map<String, Object>, List<R>> funList,
			Map<String, Object> params) {
		try {
			Integer total = funTotal.invoke();
			if (total == 0) {
				return new PageInfo<>(null);
			}
			Integer pageNum = 0, pageSize = 10;
			if (params.containsKey("pageNum")) {
				pageNum = (Integer) params.get("pageNum");

			}
			if (params.containsKey("pageSize")) {
				pageSize = (Integer) params.get("pageSize");
			}
			int pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
			if (pageNum > pages) {
				pageNum = pages;
			}
			Page<R> page = new Page<>(pageNum, pageSize, true);
			params.put("pageSize", page.getPageSize());
			params.put("startRow", page.getStartRow());

			List<R> list = funList.invoke(params);

			page.setTotal(total);
			page.addAll(list);
			PageInfo<?> pageInfo = new PageInfo<>(page);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new PageInfo<>(null);
	}

	public <R> List<R> getListPage(FunctionT1R<Map<String, Object>, List<R>> funList, Map<String, Object> params) {

		try {
			Integer pageNum = 1, pageSize = 10;
			if (params.containsKey("pageNum")) {
				pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
			}
			if (params.containsKey("pageSize")) {
				pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
			}
			Page<R> page = new Page<>(pageNum, pageSize, true);
			params.put("pageSize", page.getPageSize());
			params.put("startRow", page.getStartRow());

			List<R> list = funList.invoke(params);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}
	
	public <R> List<R> getListPage(FunctionT1R<Map<String, Object>, List<R>> funList, Map<String, Object> params,Integer pageNum,Integer pageSize) {

		try {
			if (params.containsKey("pageNum")) {
				pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
			}
			if (params.containsKey("pageSize")) {
				pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
			}
			Page<R> page = new Page<>(pageNum, pageSize, true);
			params.put("pageSize", page.getPageSize());
			params.put("startRow", page.getStartRow());

			List<R> list = funList.invoke(params);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}
}
